using System;
using System.IO;
using NUnit.Framework;
using Rhino.Mocks;
using Sedge.Core;
using Sedge.Core.App;

namespace Sedge.Tests.App
{
    [TestFixture]
    public class SuiteFinderOptionsTest
    {
        [Test]
        public void ComposeNoArgumentsNoAppConfig()
        {
            CommandLineParams clp = new CommandLineParams();
            AppConfiguration appConfiguration = new AppConfiguration();

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);
            
            Assert.IsTrue(String.IsNullOrEmpty(options.SuiteFile));
            Assert.IsTrue(String.IsNullOrEmpty(options.AppName));
        }

        [Test]
        public void ComposeCmdLineArgumentsNoAppConfig()
        {
            Services.FileSystem = (new FakeFileSystem()).FileAlwaysExists();

            CommandLineParams clp = new CommandLineParams {App = "test_app", Suite = "test_suite"};
            AppConfiguration appConfiguration = new AppConfiguration();

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(options.SuiteFile == "test_suite");
            Assert.IsTrue(options.AppName == "test_app");
        }

        [Test]
        public void ComposeCmdLineArgumentsNoFileNoAppConfig()
        {
            Services.FileSystem = (new FakeFileSystem()).FileAlwaysMiss();

            CommandLineParams clp = new CommandLineParams { App = "test_app", Suite = "test_suite" };
            AppConfiguration appConfiguration = new AppConfiguration();

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(String.IsNullOrEmpty(options.SuiteFile));
            Assert.IsTrue(options.AppName == "test_app");
        }

        [Test]
        public void ComposeNoArgumentsDefAppConfig()
        {
            Services.FileSystem = (new FakeFileSystem()).FileAlwaysExists();

            CommandLineParams clp = new CommandLineParams();
            AppConfiguration appConfiguration = new AppConfiguration
                                                {DefaultSuite = "def_suite", DefaultApplication = "def_app"};

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(options.SuiteFile == "def_suite");
            Assert.IsTrue(options.AppName == "def_app");
        }

        [Test]
        public void ComposeNoArgumentsDefAppConfigNoFile()
        {
            Services.FileSystem = (new FakeFileSystem()).FileAlwaysMiss();

            CommandLineParams clp = new CommandLineParams();
            AppConfiguration appConfiguration = new AppConfiguration { DefaultSuite = "def_suite" };

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(String.IsNullOrEmpty(options.SuiteFile));
        }

        [Test]
        public void ComposeFromSearchFolder()
        {
            string pattern = String.Concat("*.", Branding.ConfigExt);

            FakeFileSystem fakeFileSystem = (new FakeFileSystem()).FileAlwaysExists();
            fakeFileSystem.Directory.Stub(x => x.GetFiles("search", pattern, SearchOption.AllDirectories)).Return(new[] { "search_suite", "search_suite2" });
            fakeFileSystem.Directory.Stub(x => x.GetFiles("def", pattern, SearchOption.AllDirectories)).Return(new[] { "def_suite", "def_suite2" });
            Services.FileSystem = fakeFileSystem;

            CommandLineParams clp = new CommandLineParams();
            AppConfiguration appConfiguration = new AppConfiguration { SearchFolder = "search", DefaultFolder = "def" };

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(options.SuiteFile == "search_suite");
        }

        [Test]
        public void ComposeFromDefaultFolder()
        {
            string pattern = String.Concat("*.", Branding.ConfigExt);

            FakeFileSystem fakeFileSystem = (new FakeFileSystem()).FileAlwaysExists();
            fakeFileSystem.Directory.Stub(x => x.GetFiles("search", pattern, SearchOption.AllDirectories)).Return(new string[]{});
            fakeFileSystem.Directory.Stub(x => x.GetFiles("def", pattern, SearchOption.AllDirectories)).Return(new[] { "def_suite", "def_suiteX" });
            Services.FileSystem = fakeFileSystem;

            CommandLineParams clp = new CommandLineParams();
            AppConfiguration appConfiguration = new AppConfiguration { SearchFolder = "search", DefaultFolder = "def" };

            SuiteFinderOptions options = SuiteFinderOptions.Compose(clp, appConfiguration);

            Assert.IsTrue(options.SuiteFile == "def_suite");
        }
    }
}